

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class ClassAdd extends JFrame{
    public ClassAdd(String userid){

        SQLOperation sql=new SQLOperation();

        setTitle("Class Add");
        Container container=getContentPane();
        setLayout(new BorderLayout());

        JPanel titlePanel=new JPanel();

        JLabel title=new JLabel("Please complete the following information.");
        title.setFont(new Font("",Font.BOLD,20));

        titlePanel.add(title);
        add(titlePanel,BorderLayout.NORTH);

        JPanel info=new JPanel(new GridLayout(1,1,5,5));

        JPanel namePanel=new JPanel();

        namePanel.add(new JLabel("Class Name:"));
        JTextField name=new JTextField(18);
        namePanel.add(name);
        info.add(namePanel);

        add(info,BorderLayout.CENTER);


        JButton back=new JButton("Back");
        back.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
                new ClassList(userid);
            }
        });

        JButton add=new JButton("Add");
        add.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(name.getText().equals("")){
                    JOptionPane.showMessageDialog
                            (null, "Please Fill in all information!","Missing Input",
                                    JOptionPane.ERROR_MESSAGE);

                }
                else{

                    String newPK="";
                    boolean isSame=true;
                    while(isSame){

                        isSame=false;
                        newPK="ti";



                        for(int i=1;i<=5;i++){
                            newPK=newPK+""+(char)((int)(Math.random()*10)+48);
                        }
                        String[] pkID={"typeID"};
                        Object[][] pk=sql.search("DeviceType",pkID,"shanchu",1);
                        for(int i=0;i<pk.length;i++){
                            if(pk[i][0].equals(newPK)){
                                isSame=true;
                                break;
                            }
                        }
                    }




                    Object[] newInfo={newPK,name.getText(),1};

                    sql.add(newInfo,"DeviceType");


                    JOptionPane.showMessageDialog
                            (null, "Add Complete!Random DeviceType ID:"+newPK,
                                    "Success",JOptionPane.INFORMATION_MESSAGE);

                    setVisible(false);

                    new ClassList(userid);


                }


            }
        });




        JPanel buttons=new JPanel();
        buttons.add(back);
        buttons.add(add);

        add(buttons,BorderLayout.SOUTH);






        setSize(500,200);
        setResizable(false);
        setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        setVisible(true);




    }
}
